import { Injectable, Inject } from "@angular/core";
import { Router } from "@angular/router";
import { Actions, Effect, ofType } from "@ngrx/effects";
import { of } from "rxjs";
import { catchError, exhaustMap, map, tap } from "rxjs/operators";
import { ShareActions } from "../actions";
import {
  ISelectSymbolResponse,
  ISelectSymbolRequest,
  IMu,
  IZi,
  IAddOrderRequest,
  IDelOrderRequest,
  ISelectOrderRequest
} from "@/share/store/models/share";

@Injectable()
export class ShareEffects {
  constructor(
    private actions$: Actions,
    @Inject("SymbolService") private symbolService,
    @Inject("MsgService") private msgService,
    @Inject("ShareService") private shareService,
    private router: Router
  ) {}

  @Effect()
  selectSymbol$ = this.actions$.pipe(
    ofType<ShareActions.SelectSymbol>(
      ShareActions.ShareActionTypes.SelectSymbol
    ),
    map(action => action.payload.req),
    exhaustMap((req: ISelectSymbolRequest) => {
      return this.symbolService.select(req).pipe(
        map((res: any) => {
          return new ShareActions.SelectSymbolSuccess({ res });
        }),
        catchError(error => of(new ShareActions.SelectSymbolError({ error })))
      );
    })
  );

  @Effect({ dispatch: false })
  selectSymbolSuccess$ = this.actions$.pipe(
    ofType(ShareActions.ShareActionTypes.SelectSymbolSuccess),
    tap((response: any) => {
      const { payload } = response;
      // if (200 === payload.res.code) {
      //   this.msgService.success("success");
      // } else {
      //   this.msgService.error("error");
      // }
    })
  );

  @Effect({ dispatch: false })
  selectSymbolError$ = this.actions$.pipe(
    ofType(ShareActions.ShareActionTypes.SelectSymbolError),
    tap(() => {
      this.msgService.error("error");
    })
  );
}
